import { System } from "@/FrontProcessing/System/System";
import { debounce } from "stl-typescript";
import { devtool, IMouseEvent } from "cax-base";
import { MeshActor } from "@/FrontProcessing/Actors/MeshActor";
export const func_mesh_edge_selector = debounce(
  (event: IMouseEvent, sys: System) => {
    const pos = event.position;
    const point = [pos.x, pos.y, 0.0];
    // devtool.info(`🟢 mesh_edge 当前选择点: ${point}`);
    sys.app.selector.vtPicker.pick(point, sys.app.renderer);
    if (sys.app.selector.vtPicker.getActors().length === 0) {
      return 0;
    } else {
      const modelPosition = sys.app.selector.vtPicker.getMapperPosition();
      const pickedCellId = sys.app.selector.vtPicker.getCellId();
      sys.app.actorList.foreach((ac) => {
        if (ac instanceof MeshActor && ac.isDpMesh == false) {
          if (ac.containsCellId(pickedCellId)) {
            // 将网格pickedCellId转化为格宇识别的网格index
            const cellId = ac.getElemIdByCellId(pickedCellId);
            // 根据cellId 对应data中cellId找到点击的网格的顶点
            const cell = ac.getCells(cellId);
            // console.log("cell", cell);
            if (cell) {
              const data = ac.searchMeshEdgeByPoint(modelPosition, cell);
              if (data) {
                // 选中
                data.elementEdgeNodeId = data?.start[0];
                data.elementEdgeSecondNodeId = data?.end[0];
                // sys.state.currentSelectedMeshEdges.set(data.uid, data);
                ac.coverMeshEdgeColor(
                  data,
                  data?.start.slice(1),
                  data?.end.slice(1),
                  [1, 1, 0],
                  5
                );
              }
            } else {
              devtool.warn(`🟢 nodes未定义`);
            }
          }
        }
      });
    }
  },
  50,
  false
);
